package com.example.administrator.jingpingou.fragment;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.example.administrator.jingpingou.R;
import com.example.administrator.jingpingou.activity.NextThemeActivity;
import com.example.administrator.jingpingou.activity.WebTaoActivity;
import com.example.administrator.jingpingou.adapter.NextThemeTwoAdapter;
import com.example.administrator.jingpingou.bean.NextThemeBean;
import com.example.administrator.jingpingou.config.NetConfig;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2016/8/30.
 */
public class NextThemeFragmentTwo extends Fragment {
    private ListView mListView;
    private ImageView mImageAnim;
    private RelativeLayout mRelativeLayout;
    private NextThemeTwoAdapter mAdapter;
    private List<NextThemeBean.DataBean> mData;
    private String zid;
    private String num_iid;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_next_theme_two,null);
//        TextView textView = new TextView(getActivity());
//        textView.setText("TWO");
        Bundle bundle=getArguments();
        zid = bundle.getString("zid");
        initView(view);
        initData();
        setData();
        setListener();
        return view;
    }

    private void setListener() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d("num", "onItemClick: -------onitemclick");
                num_iid = mData.get(position).getNum_iid();
                NextThemeBean.DataBean dataBean = mData.get(position);
                Intent intent = new Intent();
                intent.setClass(getActivity(),WebTaoActivity.class);
                intent.putExtra("num_iid",num_iid);
                intent.putExtra("dataBean",dataBean);
                startActivity(intent);

            }
        });
    }

    private void setData() {
        initAnim();
        mListView.setAdapter(mAdapter);
    }

    private void  initAnim(){
        mImageAnim.setBackgroundResource(R.drawable.pig_fly_wait);
        final AnimationDrawable anim = (AnimationDrawable) mImageAnim.getBackground();
        mImageAnim.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                anim.start();
                return true;
            }
        });
        mRelativeLayout.removeView(mImageAnim);
        mListView.setEmptyView(mImageAnim);

    }
    private void initData() {
        mData =new ArrayList<>();
        mAdapter =  new NextThemeTwoAdapter(mData,getActivity());
        loadSupport();

//        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                mData.get(position);
//                Intent intent = new Intent(getActivity(),NextThemeActivity.class);
//                intent.putExtra("zid",zid);
//                startActivity(intent);
//            }
//        });
    }

    private void initView(View view) {
        mListView = (ListView) view.findViewById(R.id.lv_activity_next_theme_two);
        mImageAnim= (ImageView) view.findViewById(R.id.iv_fragment_next_two);
        mRelativeLayout = (RelativeLayout) view.findViewById(R.id.rl_fragment_next_two);
    }

    public void loadSupport(){
        String pathSupport = String.format(NetConfig.PATH_THEME_NEXT,zid);
        x.http().get(new RequestParams(pathSupport), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (result!=null) {
                    Gson gson = new Gson();
                    NextThemeBean nextThemeBean = gson.fromJson(result, NextThemeBean.class);
//                    currentPage++;
                    mData.addAll(nextThemeBean.getData());
                }
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
